package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.GrowthRecord;
import com.example.demo.mapper.GrowthRecordMapper;
import com.example.demo.service.GrowthRecordService;
import com.example.demo.vo.GrowthRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (GrowthRecord)表服务实现类
 *
 * @author makejava
 * @since 2025-01-27 23:57:21
 */
@Service
@Slf4j
public class GrowthRecordServiceImpl extends ServiceImpl<GrowthRecordMapper, GrowthRecord> implements GrowthRecordService {

    @Override
    public List<GrowthRecordVO> getGrowthRecord(Long childId) {
        log.info("查询儿童成长记录，childId: {}", childId);
        LambdaQueryWrapper<GrowthRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GrowthRecord::getChildId, childId);
        lambdaQueryWrapper.orderByDesc(GrowthRecord::getCreateTime);
        List<GrowthRecord> result = list(lambdaQueryWrapper);
        log.info("查询结果: {}", result);
        List<GrowthRecordVO> collect = result.stream().map(growthRecord -> {
            GrowthRecordVO growthRecordVO = new GrowthRecordVO();
            growthRecordVO.setId(growthRecord.getId());
            growthRecordVO.setHeight(growthRecord.getHeight());
            growthRecordVO.setWeight(growthRecord.getWeight());
            growthRecordVO.setDate(growthRecord.getRecordTime());
            growthRecordVO.calculateBMIAndEvaluation();
            return growthRecordVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void addGrowthRecord(GrowthRecord growthRecord) {
        save(growthRecord);
    }
}
